<template>
  <div class="new-table-style">
    <section class="search-style-box" v-if="activeTab == 2">
      <el-form ref="searchFrom" label-width="120px" size="small" :model="searchParams">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="开始时间">
              <el-date-picker v-model="searchParams.leActualPaymentDate" type="date" value-format="timestamp" :picker-options="pickerOptionsStart" clearable placeholder="请选择"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结束时间">
              <el-date-picker v-model="searchParams.geActualPaymentDate" type="date" value-format="timestamp" :picker-options="pickerOptions" clearable placeholder="请选择"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="事项">
              <el-select v-model="searchParams.bizId" clearable filterable>
                <el-option v-for="item in options.chargeList" :key="item.primaryKey" :label="item.chargeItemAliasName" :value="item.primaryKey" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-button type="primary" size="mini" class="f-right" @click="getList('')">查询</el-button>
            <el-button type="primary" size="mini" class="f-right margin-right-10" @click="reset">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </section>
    <el-table :border="true" :data="tableData" align="left" v-if="tableData.length || activeTab != 2">
      <el-table-column label="标题" prop="fundsDesc"></el-table-column>
      <el-table-column label="付款事项" prop="chargeItemName"></el-table-column>
      <el-table-column label="应付总额(元)">
        <template slot-scope="scope">
          <span>{{ (scope.row.applyAmount || 0) | fmoneyThousand }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已付款金额(元)">
        <template slot-scope="scope">
          <span>{{ scope.row.paymentAmount | fmoneyThousand }}</span>
        </template>
      </el-table-column>
      <el-table-column label="剩余未付金额(元)">
        <template slot-scope="scope">
          <span>{{ accSubtr(scope.row.applyAmount, scope.row.paymentAmount) | fmoneyThousand }}</span>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-if="total > 0" @change="pageChange" :total="total" paginationLayout="total, prev, pager, next" />

    <div class="absolute-box btn-box flex">
      <el-button class="normal-btn" size="mini" type="primary" @click="downTemplate">下载模板</el-button>
      <el-upload :show-file-list="false" style="padding: 0 10px" class="upload-demo" action accept="" :http-request="event => beforeupload(event.file)">
        <el-button type="primary" size="mini">导入</el-button>
      </el-upload>
      <el-button type="primary" size="mini" @click="exportFn">导出</el-button>
    </div>
  </div>
</template>

<script>
  import { apiPayList, apiPaysonDownload, apiPayImport, apiPayExport } from '../../../api/payManage';
  import { accSubtr } from '@/common/js/utils';

  export default {
    name: 'billTable',
    props: {
      activeTab: {
        type: String,
        default: '',
      },
      options: {
        type: Object,
        default: () => {
          return {};
        },
      },
    },
    data() {
      return {
        pickerOptions: {
          disabledDate: time => {
            if (this.searchParams.leActualPaymentDate) {
              return time.getTime() < this.searchParams.leActualPaymentDate;
            }
          },
        },
        pickerOptionsStart: {
          disabledDate: time => {
            if (this.searchParams.geActualPaymentDate) {
              return time.getTime() > this.searchParams.geActualPaymentDate;
            }
          },
        },
        searchParams: {},
        tableData: [],
        total: 0,
        currentPage: 1,
        pageSize: 10,
      };
    },
    methods: {
      init() {
        if (this.activeTab != 2) {
          this.getList();
        } else {
          this.tableData = [];
          this.currentPage = 1;
          this.total = 0;
        }
      },

      accSubtr(a, b) {
        return accSubtr(a, b);
      },

      reset() {
        this.searchParams = {};
        this.getList('');
      },

      //数据源
      async getList(more) {
        let params = {
          currentPage: more ? this.currentPage : 1,
          pageSize: this.pageSize,
          recordStatus: this.activeTab == '1' ? 1 : null,
          sourceTypeList:["contract","other"],
          ...this.searchParams,
        };
        if (this.activeTab == '1') {
          params.fundsType = '2';
        }
        let res = await apiPayList(params);
        this.tableData = res.rows || [];
        this.total = res.total || 0;
      },

      downTemplate() {
        apiPaysonDownload({
          recordStatus: 1,
        });
      },

      //分页
      pageChange(page) {
        this.currentPage = page.currentPage;
        this.getList('more');
      },

      //导入
      beforeupload(file) {
        apiPayImport(file).then(res => {
          this.$message.success('上传成功');
          this.getList('');
        });
      },

      //导出
      exportFn() {
        apiPayExport({
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          recordStatus: this.activeTab == '1' ? 1 : null,
          sourceTypeList:["contract","other"],
          ...this.searchParams,
        });
      },
    },
  };
</script>

<style lang="scss" scoped>
  .search-style-box {
    background: #fbfbfb;
    border-radius: 3px;
    padding: 20px 20px 0;
    margin-bottom: 20px;
  }

  .btn-box {
    right: 0;
    top: -45px;
  }
</style>
